package com.robusta.cityuate.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.robusta.cityuate.R;
import com.robusta.cityuate.objects.Comment;
import com.robusta.cityuate.objects.User;
import com.robusta.cityuate.utils.TimeUtils;

public class CommentItemView extends RelativeLayout {

	int screenWidth;
	private ImageView userImage;
	private ImageLoader imgLoader;
	private TextView userFullNameField;
	private TextView commentTextView;
	private TextView commentTimeView;

	public CommentItemView(Context context) {
		super(context);

	}

	public CommentItemView(Context context, AttributeSet attrs) {
		super(context, attrs);

	}

	public CommentItemView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

	}

	public CommentItemView(Context context, int width) {
		this(context);

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.comment_item, this);
		screenWidth = width;

		userImage = (ImageView) this.findViewById(R.id.comment_user_image);
		userFullNameField = (TextView) findViewById(R.id.comment_user_name);
		commentTextView = (TextView) findViewById(R.id.comment_text);
		commentTimeView=(TextView)findViewById(R.id.comment_time);

		LinearLayout.LayoutParams retItemImage1 = new LinearLayout.LayoutParams(
				10 * screenWidth / 100, 10 * screenWidth / 100);

		userImage.setLayoutParams(retItemImage1);

		imgLoader = ImageLoader.getInstance();
	}
	
	/**
	 * Bind comment data to the view 
	 * @param comment
	 */
	public void bindData(Comment comment) {
		User commentOwner = comment.getUser();
		// prepare userImage
		DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
				.displayer(new RoundedBitmapDisplayer(5)).build();
		imgLoader.displayImage("drawable://" + R.drawable.user_image_test,
				userImage, displayOptions);

		userFullNameField.setText(commentOwner.getFirst_name()+" "+commentOwner.getLast_name());
		commentTextView.setText(comment.getBody());
		commentTimeView.setText(TimeUtils.getTimeFromNow(comment.getTime()));
		
	}
	
	

}
